/**
 * 
 */
package com.fox.webapp.web.admin;

import java.util.List;

import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fox.webapp.domain.Broker;
import com.fox.webapp.domain.Policy;
import com.fox.webapp.service.IBrokerService;

/**
 * @author yockii
 *
 */
@Controller
@RequestMapping("admin/broker")
public class BrokerAdminController {
	private static final Logger logger = LoggerFactory.getLogger(BrokerAdminController.class);
	@Autowired
	private IBrokerService brokerService;

	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	///////////////////////    添加做市商    ///////////////////////////////
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String addPage(){
		return "admin/broker/add";
	}
	
	@RequestMapping(value = "add", method = {RequestMethod.POST, RequestMethod.PUT})
	@ResponseBody
	public Broker addBroker(Broker broker){
		return brokerService.addNewBroker(broker);
	}
	
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	///////////////////////    查看做市商    ///////////////////////////////
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	@RequestMapping(value = "show", method = RequestMethod.GET)
	public String showPage(Model model){
		model.addAttribute("brokerList", brokerService.getAllBrokers());
		return "admin/broker/show";
	}
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateBrokerPage(Model model, @PathVariable String id){
		model.addAttribute("broker", brokerService.getBroker(id));
		return "admin/broker/add";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String updateBroker(Model model, Broker broker){
		brokerService.updateBroker(broker);
		return showPage(model);
	}
	
	@RequestMapping(value = "remove", method = {RequestMethod.POST, RequestMethod.DELETE})
	@ResponseBody
	public boolean deleteBroker(Broker broker){
		return brokerService.deleteBroker(broker);
	}
	
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	///////////////////////    做市商政策    ///////////////////////////////
	//////////////////////////////////////////////////////////////////////
	//////////////////////////////////////////////////////////////////////
	@RequestMapping(value = "policy", method = RequestMethod.GET)
	public String policyPage(Model model, ObjectId brokerId){
		model.addAttribute("pList", brokerService.getAllPolicy(brokerId));
		return "admin/broker/policy";
	}
	
	@RequestMapping(value= "addPolicy", method = {RequestMethod.POST, RequestMethod.PUT})
	@ResponseBody
	public Policy addPolicy(Policy policy, String brokerId){
		return brokerService.addNewPolicy(policy, brokerId);
	}
	
	@RequestMapping(value = "updatePolicy", method = RequestMethod.POST)
	@ResponseBody
	public Policy updatePolicy(Policy policy){
		return brokerService.updatePolicy(policy);
	}
	
}
